using System;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.PORTALSETTING
{
    public partial class Main : PortalModuleControl
    {
        protected override void OnInit(EventArgs e)
        {
            if (!PortalSecurity.IsInRole("1"))
            {
                btnApply.Enabled = PortalSecurity.HasEditAuthorization(this.ModuleID);
            }
            
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Layouts.
                foreach (string s in Layout.GetLayoutList())
                {
                    ddlLayout.Items.Add(new ListItem(s, s));
                }

                // Themes.
                foreach (string s in Theme.GetThemeList())
                {
                    ddlTheme.Items.Add(new ListItem(s, s));
                }

                Portal portal = (Portal)Context.Items["Portal"];
                txtSiteName.Text = portal.Title;
                ddlLayout.SelectedValue = portal.Layout;
                ddlTheme.SelectedValue = portal.DefaultTheme;
                chkShowEditButton.Checked = portal.AlwaysShowEditButton;
                txtLanguage.Text = portal.Language;
                chkAllowRegister.Checked = portal.AllowRegister;
            }
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            Portal portal = (Portal)Context.Items["Portal"];
            PortalConfiguration config = new PortalConfiguration();
            config.UpdatePortalInfo(portal.ID, txtSiteName.Text, ddlLayout.SelectedValue, ddlTheme.SelectedValue, chkShowEditButton.Checked, txtLanguage.Text, chkAllowRegister.Checked);
            // Refresh.
            Response.Redirect(Request.RawUrl);
        }

        protected void ButtonEditCss_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "EditCss", "theme={0}", ddlTheme.SelectedValue);
            Response.Redirect(url);
        }
    }
}